
const app = getApp()
Page({
  data: {
    boxId:0,
    box_list:[],
    current_box_list:[],
    currentBoxSum:0,
    currentBoxes:false,
    currentBoxId:0,
    currentBoxIndex:0,
    showDetail:false,
    pageNum:1

  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        boxId:options.id
      })
    }
   
  },
  onShow: function () {
    this.getBoxList()
  },
  openbox(e){
    this.setData({
      currentBoxes:true,
      currentBoxId:e.currentTarget.dataset.id,
      currentBoxIndex:e.currentTarget.dataset.index
    })
    this.getCurrentBoxList()
  },
  changeCurrent(){
    this.setData({
      currentBoxes:false,
    })
  },
  toLastBox(){
    if (this.data.currentBoxIndex) {
      for (let i=this.data.currentBoxIndex-1;i>=-1;i--){
        if (this.data.box_list[i].surplus!=0){
          this.setData({
            currentBoxId:this.data.box_list[i].id,
            currentBoxIndex:i
          })
          this.getCurrentBoxList()
          break;
        }else{
          wx.showToast({
            icon:'none',
            title:'前面没有了'
          })
        }
      }
    }else{
      wx.showToast({
        icon:'none',
        title:'前面没有了'
      })
    }
  },
  toNextBox(){
    if (this.data.currentBoxIndex<this.data.box_list.length-1) {
      for (let i=this.data.currentBoxIndex+1;i<this.data.box_list.length;i++){
        if (this.data.box_list[i].surplus!=0){
          this.setData({
            currentBoxId:this.data.box_list[i].id,
            currentBoxIndex:i
          })
          this.getCurrentBoxList()
          break;
        }else{
          wx.showToast({
            icon:'none',
            title:'最后一个了'
          })
        }
      }
    }else {
      wx.showToast({
        icon:'none',
        title:'最后一个了'
      })
    }
  },
  getBoxList(){
    let _this = this;
    app.util.request({
      url: "api/box/findLimitAllPostcard",
      method: "get",
      data: {
        name:'',
        seriesId:_this.data.boxId,
        page: _this.data.pageNum,
        pageSize:16,
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            box_list: response.data.data.list
          })
          if(_this.data.pageNum<response.data.data.pages){
            _this.data.pageNum++
            _this.getBoxList()
          }
        }
      }
    })
  },
  getCurrentBoxList(){
    let _this = this;
    _this.setData({
      current_box_list:[]
    })
    wx.showLoading({
      title: '加载中',
    })
    app.util.request({
        url: "api/postcard/findALLByBoxId",
        method: "get",
        data: {
          boxId: this.data.currentBoxId
      },
        success(response) {
            if (response.data.code == 0) {
                _this.setData({
                  surplus:response.data.data[0].surplus,
                  current_box_list: response.data.data[0].postcard
                })
            }
            wx.hideLoading()
        }
    })
  },
  getForward(e){
    let number = e.currentTarget.dataset.num
    if(number>this.data.surplus){
      wx.showToast({
        icon:'none',
        title: '库存不足了！',
      })
    }else{
      wx.navigateTo({
        url: '/pages/order/create-order/create-order?id='+this.data.currentBoxId+'&number='+number+'&type=2',
      })
    }
  },
  back(){
    if(this.data.currentBoxes){
      this.changeCurrent()
      this.getCurrentBoxList()
    }else
      wx.navigateBack({
        delta: 1
      })
  },
  toBoxOrder(e){
    let number=e.currentTarget.dataset.num?e.currentTarget.dataset.num:this.data.currentBoxSum
    wx.navigateTo({
      url: '/pages/order/create-order/create-order?id='+this.data.currentBoxId+'&number='+ number+'&type=1',
    })
  },
  onClose(){
    this.setData({
      showDetail:false
    })
  },
})
